﻿//
//  SwitchCityController.cpp
//  Weather
//
//  Created by 朱建 31990839@qq.com on 16-08-18.
//  Copyright (c) 2014 http://www.9miao.com All rights reserved.


#include "SwitchCityController.h"
#include "RootWindow.h"


CSwitchCityController::CSwitchCityController()
: m_nCurSelectRow1(0)
, m_nCurSelectRow2(0)
{
}


CSwitchCityController::~CSwitchCityController()
{
}

void CSwitchCityController::viewDidLoad()
{
	// Do any additional setup after loading the view from its nib.
	CAPickerView* pickView = CAPickerView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_B(40, 180)));
    pickView->setTag(100);
	pickView->setFontSizeNormal(36);
	pickView->setFontSizeSelected(64);
    pickView->setPickerViewDataSource(this);
    pickView->setPickerViewDelegate(this);
	this->getView()->addSubview(pickView);
	pickView->reloadAllComponents();

	CAButton* switchButton = CAButton::createWithLayout(DLayout(DHorizontalLayout_L_R(60, 60), DVerticalLayout_B_H(40, 100)), CAButtonTypeRoundedRect);
	switchButton->setTitleForState(CAControlStateAll, UTF8("切换城市"));
	switchButton->setTitleColorForState(CAControlStateAll, ccc4(102, 51, 51, 255));
	switchButton->setBackGroundViewForState(CAControlStateAll, CAImageView::createWithImage(CAImage::create("r/btn.png")));
	switchButton->addTarget(this, CAControl_selector(CSwitchCityController::saveSelectCity), CAControlEventTouchUpInSide);
	this->getView()->addSubview(switchButton);

	performSelector(callfunc_selector(CSwitchCityController::setDefaultCity), 0.1f);
	CAApplication::getApplication()->getKeypadDispatcher()->addDelegate(this);
}

void CSwitchCityController::viewDidUnload()
{
	// Release any retained subviews of the main view.
	CAApplication::getApplication()->getKeypadDispatcher()->removeDelegate(this);
}

void CSwitchCityController::viewDidAppear()
{
	getNavigationController()->setNavigationBarHidden(false, false);
}

void CSwitchCityController::viewDidDisappear()
{
	
}

void CSwitchCityController::keyBackClicked()
{
	RootWindow::getInstance()->keyBackClicked();
}

void CSwitchCityController::setDefaultCity()
{
	CAPickerView* pickView = (CAPickerView*)this->getView()->getSubviewByTag(100);
	if (pickView)
	{
		RootWindow::getInstance()->getCurProvinceAndCity(m_nCurSelectRow1, m_nCurSelectRow2);
		pickView->selectRow(m_nCurSelectRow1, 0);
		pickView->reloadComponent(0, 1);
		pickView->selectRow(m_nCurSelectRow2, 1);
	}
}

void CSwitchCityController::saveSelectCity(CAControl* target, CCPoint point)
{
	RootWindow::getInstance()->setCurProvinceAndCity(m_nCurSelectRow1, m_nCurSelectRow2);
	CAAlertView* alertView = CAAlertView::createWithText(UTF8("提示"), UTF8("切换城市成功"), UTF8("确定"), NULL);
	alertView->setTarget(this, CAAlertView_selector(CSwitchCityController::onClickBtnSaveOk));
	alertView->show();
}

void CSwitchCityController::onClickBtnSaveOk(int index)
{
	RootWindow::getInstance()->BackToPreviousController();
}

void CSwitchCityController::didSelectRow(CAPickerView* pickerView, unsigned int row, unsigned int component)
{
	if (component == 0)
	{
		m_nCurSelectRow1 = row;
		m_nCurSelectRow2 = 0;
		((CAPickerView*)this->getView()->getSubviewByTag(100))->reloadComponent(0, 1);
	}
	if (component == 1)
	{
		m_nCurSelectRow2 = row;
	}
}

unsigned int CSwitchCityController::numberOfComponentsInPickerView(CAPickerView* pickerView)
{
    return 2;
}

unsigned int CSwitchCityController::numberOfRowsInComponent(CAPickerView* pickerView, unsigned int component)
{
	const std::vector< std::vector<std::string> >& AllCitys = RootWindow::getInstance()->getAllCitys();
	if (component == 0)
	{
		return AllCitys.size();
	}
	if (component == 1)
	{
		const std::vector<std::string>& v = AllCitys[m_nCurSelectRow1];
		if (v.size() > 1)
		{
			return v.size() - 1;
		}
		else
		{
			return 1;
		}
	}
	return 1;
}

float CSwitchCityController::widthForComponent(CAPickerView* pickerView, unsigned int component)
{
	return (RootWindow::getInstance()->getFrame().size.width-60) / 2;
}

float CSwitchCityController::rowHeightForComponent(CAPickerView* pickerView, unsigned int component)
{
	return 80;
}

const char* CSwitchCityController::titleForRow(CAPickerView* pickerView, unsigned int row, unsigned int component)
{
	const std::vector< std::vector<std::string> >& AllCitys = RootWindow::getInstance()->getAllCitys();
	if (component == 0)
	{
		return AllCitys[row][0].c_str();
	}
	if (component == 1)
	{
		const std::vector<std::string>& v = AllCitys[m_nCurSelectRow1];
		if (v.size() > 1)
		{
			return v[row+1].c_str();
		}
		else
		{
			return v[0].c_str();
		}
	}
}


